<template>
	<view class="immediate_settlement_mian">

		<!-- 购物结算主区域 -->
		<view class="shopping_order_main">
			<!-- 这里是页面主体内容区域 -->
			<view class="return_pre_page">
				<uni-icons type="arrow-left" size="30" @click="returnPrePage"></uni-icons>
			</view>
			<view class="shopping_order_text">
				购物结算
			</view>
			<view class="address_main">
				<view class="address_text_and_icon" v-if="!isHaveReceiveAddress">
					<view class="address_text">
						请添加收货地址
					</view>
					<view class="add_receive_address_icon" @click="selectAddressModal">
						<uni-icons type="right" size="20" color="#9d9d9d"></uni-icons>
					</view>
				</view>
				<view class="have_address_main" v-else>
					<view class="provice_city_county">
						{{receiveAddress.province}}&nbsp;&nbsp;{{receiveAddress.city}}&nbsp;&nbsp;{{receiveAddress.county}}
					</view>
					<view class="detail_address_text_icon">
						<view class="detail_address_text">
							{{receiveAddress.detailAddress}}
						</view>
						<view class="change_address_icon" @click="selectAddressModal">
							<uni-icons type="right" color="#9e9e9e" size="24"></uni-icons>
						</view>
					</view>
					<view class="receive_person">
						{{receiveAddress.receivePersonName}}&nbsp;&nbsp;{{receiveAddress.phone}}
					</view>
				</view>
			</view>

			<!-- 虚线 -->
			<view class="dashed-line"></view>

			<!-- 订单的所有商品 -->
			<view class="order_main">
				<view class="order_per" v-for="(item,index) in orderGoods" :key="index">
					<view class="left_good_image">
						<image :src="item.goodimg" mode="aspectFill" class="good_image"></image>
					</view>
					<view class="right_good">
						<view class="right_good_name_quantity">
							<view class="right_good_name">
								{{item.goodName}}
							</view>
							<view class="right_good_quantity">
								X&nbsp;{{item.selectedNum}}
							</view>
						</view>
						<view class="right_good_style">
							{{item.selectStyle}}
						</view>
						<view class="right_good_price_record">
							<view class="right_good_price">
								￥{{item.price}}&nbsp;&nbsp;
							</view>
							<view class="right_good_icon">
								<image src="../../static/icons/integral.png" mode="aspectFill" class="records_icon">
								</image>
								{{item.records}}
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="order_footer_desc">
				<!-- 商品总额 -->
				<view class="goods_all_price">
					<view class="goods_all_price_left">
						商品总额
					</view>
					<view class="goods_all_price_text">
						￥{{totalPrice}}
					</view>
				</view>
				<!-- 运费 -->
				<view class="goods_all_price">
					<view class="goods_all_price_left">
						运费
					</view>
					<view class="goods_all_price_text">
						{{freight}}
					</view>
				</view>
				<!-- 可用积分 -->
				<view class="goods_all_price">
					<view class="goods_all_price_left">
						<view class="goods_all_price_left_text">
							积分
						</view>
						<view class="goods_all_price_left_question" @click="showRecordsTips">
							?
						</view>
					</view>
					<view class="goods_all_price_text">
						<text class="allready_user_record_text">已使用</text>&nbsp;{{userRecords}}&nbsp;
						<uni-icons type="right" @click="recordsTopShow"></uni-icons>
					</view>
				</view>
			</view>
		</view>


		<!-- 底部（显示价格和立即下单按钮） -->
		<view class="footer_price_btn">
			<view class="left_price">
				￥{{orderTotalPrice}}
			</view>
			<view class="immediate_settlement_bnt" @click="placeOrder">
				<button>立即下单</button>
			</view>
		</view>


		<!-- 点击积分问号打开弹窗 -->
		<uni-popup ref="recordsPopup" type="bottom">
			<view class="records_main">
				<view class="my_records">
					我的积分：&nbsp;<text class="my_records_num">{{myRecords}}</text>
				</view>
				<view class="records_input_view">
					<uni-easyinput type="number" v-model="inputRecord" placeholder="请输入您要使用的积分"></uni-easyinput>
				</view>
				<view class="input_record_btn" @click="calculateRecord">
					<button>确定</button>
				</view>
			</view>
		</uni-popup>


		<!-- 选择收货地址弹窗 -->
		<uni-popup ref="addressPopup" type="bottom" :mask-click="false">
			<view class="select_address_main">
				<view class="my_address_text_icon">
					<view class="my_address_text">
						我的地址
					</view>
					<view class="my_address_icon" @click="closeAddressPopup">
						<uni-icons type="closeempty" size="26" color="#868686"></uni-icons>
					</view>
				</view>
				<view class="address_content" v-if="isHaveReceiveAddress">
					<image src="@/static/image/empty_box.png" mode="aspectFill" class="empty_box_image"></image>
				</view>
				<view class="address_content1" v-else>
					<view class="address_content_per" v-for="(item,index) in addressArray" :key="index">
						<view class="is_checked_radio">
							<label>
								<radio :checked="item.isSelected" active-background-color="#66596b"
									@click="radioClick(index)" />
							</label>
						</view>
						<view class="right_address_detail_content">
							<view class="default_name_phone_address" @click="selectThisAddress(index)">
								<view class="default_name_phone">
									<view class="default_selected_label" v-if="item.isDefault">
										<uni-tag :inverted="true" text="默认" type="success" />
									</view>
									<view class="addressee">
										{{item.addressee}}
									</view>
									<view class="address_phone">
										{{item.phone}}
									</view>
								</view>
								<view class="detail_address">
									{{item.province}}&nbsp;{{item.city}}&nbsp;{{item.county}}&nbsp;{{item.detailAddress}}
								</view>
							</view>
							<view class="edit_icon" @click="toEditAddress(item)">
								<uni-icons type="compose" size="24"></uni-icons>
							</view>
						</view>
					</view>
				</view>
				<view class="add_new_address_btn" @click="gotoCreateNewAddress">
					<button>创建新地址</button>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 这里可以定义组件的数据
				totalPrice: '0', // 订单总价
				isHaveReceiveAddress: false, // 是否有收货地址
				receiveAddress: {
					province: '浙江省',
					city: '金华市',
					county: '永康市',
					detailAddress: '石柱镇XX村XX路21号',
					receivePersonName: '王志强',
					phone: '18387008391'
				}, // 收货地址
				orderGoods: [], // 购物车过来的商品
				userRecords: 0, // 用户的可用积分
				freight: '已免运费',
				orderTotalPrice: '0', // 订单的总价（商品总额还要加上运费，如果使用了积分还要减去积分：1元=10积分）
				myRecords: 35, // 我的积分
				inputRecord: '', // 输入的积分
				addressArray: [{
						addressAid: 1,
						isSelected: true,
						isDefault: true,
						addressee: '范占国',
						phone: '+86 18387008391',
						province: '浙江省',
						city: '金华市',
						county: '永康市',
						detailAddress: '前仓镇金鸡路170号',
					},
					{
						addressAid: 2,
						isSelected: false,
						isDefault: false,
						addressee: '李慧',
						phone: '+86 18387547345',
						province: '云南省',
						city: '楚雄市',
						county: '南华县',
						detailAddress: '王东村02号',
					},
					{
						addressAid: 3,
						isSelected: false,
						isDefault: false,
						addressee: '王林',
						phone: '+86 18669116009',
						province: '云南省',
						city: '曲靖市',
						county: '宣威市',
						detailAddress: '南京路180号',
					},
				], // 我的所有地址
			};
		},
		methods: {
			// 获取购物车传递过来的所有商品
			getCartGoods() {
				let order = uni.getStorageSync('order')
				this.totalPrice = order.totalPrice;
				this.orderGoods = order.cartGoods;
			},
			// 让收货地址的电话号码中间四位变为  *  号
			chengePhoneFormat() {
				if (this.isHaveReceiveAddress) {
					let phone = this.receiveAddress.phone;
					this.receiveAddress.phone = phone.substring(0, 3) + '****' + phone.substring(7);
				}
			},
			// 返回上一页
			returnPrePage() {
				uni.navigateBack();
			},
			// 计算订单的总额（订单的总价（商品总额还要加上运费，如果使用了积分还要减去积分：1元=10积分））
			calculateOrderTotalPrice() {
				if (this.freight === '已免运费') {
					if (this.userRecords === 0) {
						this.orderTotalPrice = this.totalPrice;
					} else {
						this.orderTotalPrice = this.totalPrice - this.userRecords / 10;
					}
				} else {
					if (this.userRecords === 0) {
						this.orderTotalPrice = Number(this.totalPrice) + Number(this.freight);
					} else {
						this.orderTotalPrice = Number(this.totalPrice) + Number(this.freight) + Number(this.userRecords /
							10);
					}
				}
			},

			// 点击立即下单按钮
			placeOrder() {
				if (this.isHaveReceiveAddress) {
					// 去支付页面
				} else {
					uni.showToast({
						title: '请添加收货地址',
						icon: 'none'
					})
				}
			},

			// 点击已使用积分后面的弹框提示
			recordsTopShow() {
				this.$refs.recordsPopup.open();
			},

			// 点击积分后面的问号？弹出提示
			showRecordsTips() {
				uni.showToast({
					title: '1元 = 10积分',
					icon: 'none'
				})
			},

			// 点击积分弹窗按钮确认，计算积分
			calculateRecord() {
				let inputRecord = parseInt(this.inputRecord);
				if (inputRecord > this.myRecords) {
					uni.showToast({
						title: '您输入的积分大于您的全部积分，请重新输入或者去充值积分！',
						icon: 'none'
					})
				} else if (inputRecord >= 0 && inputRecord <= this.myRecords) {
					this.myRecords -= inputRecord;
					this.$refs.recordsPopup.close();
					this.userRecords = inputRecord;
					// 计算订单的总额（订单的总价（商品总额还要加上运费，如果使用了积分还要减去积分：1元=10积分））
					this.calculateOrderTotalPrice();
				}
			},

			// 选择收货地址，弹窗
			selectAddressModal() {
				this.changeAddressPhoneFormat();
				this.$refs.addressPopup.open();
			},

			// 点击关闭地址弹窗
			closeAddressPopup() {
				this.$refs.addressPopup.close();
			},

			// 把地址列表里面的电话格式改成：182****8983 这种格式的
			changeAddressPhoneFormat() {
				this.addressArray.forEach((item) => {
					item.phone = item.phone.substring(0, 7) + '****' + item.phone.substring(11);
				})
			},

			// 点击地址列表中的某一项，选中地址
			selectThisAddress(index) {
				// 地址列表index那一项的isSelected改为true
				this.addressArray[index].isSelected = true;
				// 购物结算页面isHaveReceiveAddress = true
				this.isHaveReceiveAddress = true;
				// 赋值购物结算页面的收货地址
				const _this = this;
				_this.receiveAddress = {
					province: _this.addressArray[index].province,
					city: _this.addressArray[index].city,
					county: _this.addressArray[index].county,
					detailAddress: _this.addressArray[index].detailAddress,
					receivePersonName: _this.addressArray[index].addressee,
					phone: _this.addressArray[index].phone
				};
				uni.showToast({
					title: '更换地址成功！',
					icon: 'none'
				})
				_this.$refs.addressPopup.close();
			},

			// 去创建新地址
			gotoCreateNewAddress() {
				uni.navigateTo({
					url: '/pages/tabBar/template/createNewAddressPage?addressAid=0'
				})
			},
			
			// 去编辑地址
			toEditAddress(item){
				uni.navigateTo({
					url: `/pages/tabBar/template/createNewAddressPage?addressAid=${item.addressAid}`
				})
			},

			radioClick(index) {
				// 先将所有地址的选中状态设为false
				this.addressArray.forEach((address) => {
					address.isSelected = false;
				});
				this.selectThisAddress(index);
			},

		},
		onLoad() {
			// 获取购物车传递过来的所有商品
			this.getCartGoods();

			// 让收货地址的电话号码中间四位变为  *  号
			this.chengePhoneFormat();

			// 计算订单的总额（订单的总价（商品总额还要加上运费，如果使用了积分还要减去积分：1元=10积分））
			this.calculateOrderTotalPrice();
		},
	};
</script>

<style scoped>
	/* 这里是组件的样式 */
	.immediate_settlement_mian {
		padding-top: 60rpx;
	}

	.footer_price_btn {
		width: 100%;
		height: 150rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.left_price {
		margin-left: 30rpx;
		font-size: 40rpx;
		font-weight: bold;
	}

	.immediate_settlement_bnt {
		margin-right: 30rpx;
	}

	.immediate_settlement_bnt button {
		width: 300rpx;
		border-radius: 60rpx;
		background-color: #66596b;
		color: #fff;
	}

	.shopping_order_main {
		padding: 30rpx;
	}


	.shopping_order_text {
		margin-top: 20rpx;
		font-size: 58rpx;
		font-weight: bold;
	}

	.address_main {
		margin-top: 60rpx;
	}

	.address_text_and_icon {
		display: flex;
		justify-content: space-between;
	}

	.address_text {
		font-size: 30rpx;
	}


	/* 两种颜色交替更换的一条虚线 */
	.dashed-line {
		margin-top: 30rpx;
		height: 4rpx;
		background-image: linear-gradient(to right, #01c6c1 0rpx, #01c6c1 20rpx, transparent 20rpx, transparent 34rpx, #dfb0cd 34rpx, #dfb0cd 54rpx, transparent 54rpx, transparent 68rpx);
		background-size: 68rpx 4rpx;
		background-repeat: repeat-x;
	}

	.detail_address_text_icon {
		margin-top: 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.detail_address_text {
		font-size: 40rpx;
		font-weight: bold;
	}

	.receive_person {
		margin-top: 30rpx;
	}

	.order_main {
		margin-top: 50rpx;
		margin-bottom: 150rpx;
	}

	.order_per {
		display: flex;
		justify-content: space-between;
		margin-bottom: 50rpx;
	}

	.good_image {
		width: 220rpx;
		height: 220rpx;
	}

	.right_good_name_quantity {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.right_good_name {
		font-size: 40rpx;
	}

	.right_good_style {
		color: #7c7c7c;
		font-size: 30rpx;
		margin-bottom: 28rpx;
	}

	.right_good_price_record {
		display: flex;
	}

	.records_icon {
		width: 30rpx;
		height: 30rpx;
	}

	.right_good {
		width: 65%;
		height: 220rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.goods_all_price {
		margin-bottom: 40rpx;
		display: flex;
		justify-content: space-between;
		font-size: 36rpx;
	}

	.goods_all_price_left {
		display: flex;
		align-items: center;
	}

	.order_footer_desc {
		margin-bottom: 200rpx;
	}

	.goods_all_price_left_question {
		width: 35rpx;
		height: 35rpx;
		color: #fff;
		border-radius: 50%;
		background-color: #66596b;
		font-size: 28rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 10rpx;
	}

	.records_main {
		width: 100%;
		height: 500rpx;
		background-color: #fff;
		border-radius: 30rpx 30rpx 0 0;
	}

	.my_records {
		margin-left: 30rpx;
		padding-top: 50rpx;
		text-align: center;
		font-size: 30rpx;
	}

	.my_records_num {
		font-size: 40rpx;
		font-weight: bold;
		color: #01c6c1;
	}

	.records_input_view {
		margin-top: 30rpx;
	}

	.input_record_btn {
		margin-top: 100rpx;
	}

	.input_record_btn button {
		background-color: #01c6c1;
	}

	.allready_user_record_text {
		font-size: 30rpx;
		color: #a3a3a3;
	}

	.select_address_main {
		width: 100%;
		height: 1300rpx;
		background-color: #fff;
		border-radius: 30rpx 30rpx 0 0;
	}

	.my_address_text_icon {
		padding: 40rpx;
		display: flex;
		justify-content: space-between;
		font-size: 40rpx;
	}

	.address_content {
		text-align: center;
		width: 100%;
		height: 90vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.address_content1 {
		padding-left: 30rpx;
		padding-right: 30rpx;
		width: 100%;
		height: 1000rpx;
	}

	.address_content_per {
		display: flex;
		margin-bottom: 50rpx;
		align-items: center;
	}

	.empty_box_image {
		width: 200rpx;
		height: 200rpx;
	}

	.add_new_address_btn button {
		font-size: 45rpx;
		border-radius: 80rpx;
		background-color: #66596b;
		color: #fff;
	}

	.add_new_address_btn {
		padding-left: 30rpx;
		padding-right: 30rpx;
	}

	.default_name_phone {
		display: flex;
	}

	.default_selected_label {
		margin-right: 20rpx;
	}

	.right_address_detail_content {
		display: flex;
		margin-left: 20rpx;
		width: 80%;
		justify-content: space-between;
	}

	.default_name_phone_address {
		width: 86%;
	}

	.address_phone {
		margin-left: 20rpx;
		color: #a3a3a3;
	}

	.addressee {
		font-size: 35rpx;
		font-weight: bold;
	}

	.detail_address {
		margin-top: 20rpx;
		line-height: 1.5;
	}

	.edit_icon {
		display: flex;
		align-items: center;
	}
</style>